/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jtwitt.client.gui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.DefaultListModel;
import jtwitt.server.model.AppUser;
import jtwitt.server.model.FollowerPK;
import jtwitt.socket.ClientThread;
import jtwitt.util.Actions;

/**
 * Allows the user to handle Followers and Following
 * @author Nikos
 */
public class EditFollow extends javax.swing.JDialog {

    private Actions action;
    private AppUser appUser;
    private List<AppUser> followList = new ArrayList<>();
    private ClientThread cThread;
    private DefaultListModel followListModel = new DefaultListModel();
    private int selectedIndex = 0;
    
    /**
     * Creates new form EditFollow
     */
    public EditFollow(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    //action 0-> follower
    /**
     * Creates new form EditFollow
     * @param parent Frame the parent Frame MainClient
     * @param modal boolean 
     * @param action Actions 
     * @param appUser AppUser object that corresponds to the current user
     * @param cThread ClientThread  the currently active ClientThread that handles this connection
     */
    public EditFollow(java.awt.Frame parent, boolean modal,Actions action,AppUser appUser,ClientThread cThread) {
        super(parent, modal);
        initComponents();
        cThread.setFollowEdit(this);
        this.appUser = appUser;
        this.cThread = cThread;  
        this.action = action;   
        jLabelError.setText("");
        loadList();
    }
                          
     private void loadList(){   
         
         cThread.setAction(action);
         cThread.wakeMe();        
     }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jListFollow = new javax.swing.JList();
        jButtonDelete = new javax.swing.JButton();
        jButtonClose = new javax.swing.JButton();
        jLabelError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jListFollow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListFollowMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListFollow);

        jButtonDelete.setText("Delete");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jButtonClose.setText("Close");
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });

        jLabelError.setForeground(new java.awt.Color(255, 0, 0));
        jLabelError.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonClose)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonDelete)
                    .addComponent(jButtonClose)
                    .addComponent(jLabelError))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jListFollowMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListFollowMouseClicked
        selectedIndex = jListFollow.getSelectedIndex();
    }//GEN-LAST:event_jListFollowMouseClicked

    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        lockControls();
        cThread.setAction(Actions.DELETE_FOLLOW);
        cThread.wakeMe();
    }//GEN-LAST:event_jButtonDeleteActionPerformed

    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
       dispose();
    }//GEN-LAST:event_jButtonCloseActionPerformed
  
    /**
     * 
     * @return
     */
    public FollowerPK getDeletedFollowPK(){
        
        if(action == Actions.LOAD_FOLLOWER_PROFILES ){            
            return getDeletedFollowerPK();
        }
        else{
            return getDeletedFollowingPK();
        }
    }    
    
    private FollowerPK getDeletedFollowerPK(){
       int followId = ((AppUser)followListModel.get(selectedIndex)).getUserId();
       return new FollowerPK(followId, appUser.getUserId());
   }
    private FollowerPK getDeletedFollowingPK(){
       int followId = ((AppUser)followListModel.get(selectedIndex)).getUserId();
       return new FollowerPK(appUser.getUserId(), followId);
   }
    /**
     * Fills the list of Followers or Following
     */
    public void fillList(){
        followListModel.clear();            
        Iterator it = followList.iterator();            
        while(it.hasNext()){
            followListModel.addElement(it.next());  
        }
       jListFollow.setModel(followListModel);
       jListFollow.setSelectedIndex(0);
       repaint();
    }

    /**
     * 
     * @return
     */
    public AppUser getAppUser() {
        return appUser;
    }

    /**
     * Assigns a List<AppUser> to the local variable
     * @param followList List<AppUser>
     */
    public void setFollowList(List<AppUser> followList) {
        this.followList = followList;
    }
     /**
     * Disable controls until actions performed
     */
    public void lockControls(){
        jButtonClose.setEnabled(false);
        jButtonDelete.setEnabled(false);        
    }
    /**
     * Re-enable controls after actions performed
     */
    public void unlockControls(){
        jButtonClose.setEnabled(true);
        jButtonDelete.setEnabled(true);        
    }
     /**
     *
     */
    public void refreshList(){
         followList.remove(selectedIndex);
         fillList();
     }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonDelete;
    public javax.swing.JLabel jLabelError;
    private javax.swing.JList jListFollow;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
